<?php
/**
 * Author: Traveler
 * Time  : 2021/9/6 16:24
 */

namespace app\service;


use app\ErrorCode;
use app\model\StaffModel;
use sunshine\basic\BaseService;
use sunshine\exceptions\ApiException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class MainService extends BaseService
{
    /**
     * 登陆后获取完整菜单信息
     *
     * @param int $id
     * @return array
     */
    public function getMainProfile(int $id): array
    {
        $staffService = new StaffService();
        $roleService  = new RoleService();
        $userInfo     = $staffService->find($id);

        if (!$userInfo) {
            throw new ApiException(ErrorCode::DATA_NOT_FOUND);
        }

        $userMenus = $roleService->getRoleMenus($userInfo['role']['id']);

        return compact('userInfo', 'userMenus');
    }

    /**
     * 获取公司相关统计信息
     *
     * @return array
     */
    public function getCompanyStatistics(): array
    {
        $icafe      = (new IcafeService())->getCountByWhere([]);
        $user       = (new UserService())->getCountByWhere([]);
        $staff      = (new StaffService())->getCountByWhere([]);
        $department = (new DepartmentService())->getCountByWhere([]);

        return compact('icafe', 'user', 'staff', 'department');
    }

    /**
     * 获取网吧相关统计信息
     *
     * @return array
     */
    public function getIcafeStatistics(): array
    {
        $neimenggu = [];
        $category  = [];
        $hohhot    = [];

        $regionService        = new RegionService();
        $icafeCategoryService = new IcafeCategoryService();
        $icafeService         = new IcafeService();

        $cityList = $regionService->getList(150000);
        foreach ($cityList as $city) {
            $neimenggu[] = [
                'name'  => $city['name'],
                'value' => $icafeService->getCountByCity($city['id'])
            ];
        }

        $categoryList = $icafeCategoryService->getList();
        foreach ($categoryList as $cate) {
            $category[] = [
                'name'  => $cate['name'],
                'value' => $icafeService->getCountByCategory($cate['id'])
            ];
        }

        $districtList = $regionService->getList(150100);
        foreach ($districtList as $district) {
            $hohhot[] = [
                'name'  => $district['name'],
                'value' => $icafeService->getCountByDistrict($district['id'])
            ];
        }

        return compact('neimenggu', 'category', 'hohhot');
    }

    /**
     * 修改密码
     *
     * @param int   $id
     * @param array $data
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function changePassword(int $id, array $data): bool
    {
        $staff = StaffModel::find($id);

        if (!$staff) {
            throw new ApiException(ErrorCode::DATA_NOT_FOUND);
        }

        if ($data['oldPassword'] !== $staff['password']) {
            throw new ApiException(ErrorCode::INVALID_OLD_PASSWORD);
        }

        if ($data['oldPassword'] === $data['newPassword']) {
            throw new ApiException(ErrorCode::SAME_PASSWORD);
        }

        return $staff->save(['password' => $data['newPassword']]);
    }

}